﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;

namespace DeloUtils
{
    public partial class frmConnect : Form
    {

        public frmConnect()
        {
            InitializeComponent();
        }

        private void btnConnect_Click(object sender, EventArgs e)
        {
            if (cmbSUDB.SelectedIndex == -1)
            {
                cmbSUDB.Focus();
                MessageBox.Show(this, "Не указан параметр", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Stop);
                return;
            }
            if (txtServer.Text == "")
            {
                txtServer.Focus();
                MessageBox.Show(this, "Не указан параметр", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Stop);
                return;
            }
            if (txtOwner.Text == "")
            {
                txtOwner.Focus();
                MessageBox.Show(this, "Не указан параметр", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Stop);
                return;
            }
            if (txtPassword.Text == "")
            {
                txtPassword.Focus();
                MessageBox.Show(this, "Не указан параметр", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Stop);
                return;
            }
            String ConnectionString;
            OleDbConnection connection = new OleDbConnection();
            if (cmbSUDB.SelectedIndex == 0)
            {
                ConnectionString = "Provider=SQLOLEDB.1;Initial Catalog="+ txtOwner.Text+"_DB;Data Source=" + txtServer.Text + ";Persist Security Info=True;User ID=" + txtOwner.Text + ";Password=" + txtPassword.Text + ";Pooling=True;Connect Timeout=10";
                ((frmMain)(this.Owner)).SqlPrefix = "";
            }
            else
            {
                ConnectionString = "Provider=OraOLEDB.Oracle;Data Source=" + txtServer.Text + ";User ID=" + txtOwner.Text + ";Password=" + txtPassword.Text + ";";
                ((frmMain)(this.Owner)).SqlPrefix = txtOwner.Text + ".";
            }

            try
            {
                connection.ConnectionString = ConnectionString;
                connection.Open();
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, "Не удалось подключится! \r\nПроверьте параметры и попробуйте еще раз.\r\n"+ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);                
            }
            if (connection.State == ConnectionState.Open)
            {
                MessageBox.Show(this, "Подключение успешно!" , this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
                ((frmMain)(this.Owner)).connection = connection;
                this.Hide();
            }
        }

   
    }
}
